package tjs.study.notes.eureka.zool.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.apache.log4j.MDC;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * Created by Nicky.Tang on 2021/12/18 5:00 下午
 *
 * @since 02.12.10
 */
@Component
public class LogFilter extends ZuulFilter {

    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return -1;
    }

    @Override
    public boolean shouldFilter() {
        //根据配置控制是否开启过滤器
        return true;
    }

    @Override
    public Object run() {
        //链路追踪id
        String traceId = UUID.randomUUID().toString();
        MDC.put("LOG_TRACE_ID", traceId);
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.addZuulRequestHeader("LOG_TRACE_ID", traceId);
        return true;
    }
}
